import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:tender_flutter/constant/resource_path.dart';
import 'package:tender_flutter/constant/theme_colors.dart';
import 'package:tender_flutter/utils/screen_adaper.dart';

class FormOptionItem extends StatelessWidget {
  const FormOptionItem(
      {this.key,
      this.title,
      this.placeholder,
      this.keyboardType,
      this.inputOnly = false,
      this.controller,
      this.noEmpty,
      this.content,
      this.focusNode,
      this.onChangeCallback,
      this.onSaveCallback,
      this.selectCallback,
      this.inputFormatters})
      : super(key: key);

  final Key key;

  final FocusNode focusNode;

  /// 标题
  final String title;

  /// 占位符
  final String placeholder;

  /// 键盘类型
  final TextInputType keyboardType;

  /// 只能输入，否则为选项
  final bool inputOnly;

  /// 是否必须有值
  final bool noEmpty;

  /// 内容
  final String content;

  /// controller
  final TextEditingController controller;

  /// Formatter
  final List<TextInputFormatter> inputFormatters;

  /// 文本框输入内容改变的回调
  final ValueChanged onChangeCallback;

  /// 表单提交时回调
  final FormFieldSetter onSaveCallback;

  /// 当为选项是选择回调
  final GestureTapCallback selectCallback;

  @override
  Widget build(BuildContext context) {
    return _getOptionsItem(context);
  }

  /// 获取选择类型item
  Widget _getOptionsItem(BuildContext context) {
    return Container(
        margin: EdgeInsets.symmetric(
            vertical: ScreenAdapter.height(20),
            horizontal: ScreenAdapter.width(30)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            Container(
                width: ScreenAdapter.width(150),
                child: Text(title,
                    style: TextStyle(
                        color: ThemeColors.colorType1,
                        fontSize: ScreenAdapter.sp(30)))),
            Expanded(
                child: Container(
              height: ScreenAdapter.height(80),
              padding: EdgeInsets.only(
                  left: ScreenAdapter.width(30),
                  right: ScreenAdapter.width(30)),
              decoration: BoxDecoration(
                  border:
                      new Border.all(width: 1, color: ThemeColors.colorType39),
                  borderRadius: BorderRadius.circular(ScreenAdapter.width(10))),
              child: Row(
                children: [
                  Expanded(
                      child: TextFormField(
                    onTap: selectCallback,
                    decoration: InputDecoration(
                      hintText: placeholder,
                      focusedBorder: OutlineInputBorder(
                          borderSide:
                              BorderSide(width: 0, color: Colors.transparent)),
                      disabledBorder: OutlineInputBorder(
                          borderSide:
                              BorderSide(width: 0, color: Colors.transparent)),
                      enabledBorder: OutlineInputBorder(
                          borderSide:
                              BorderSide(width: 0, color: Colors.transparent)),
                      border: OutlineInputBorder(
                          borderSide:
                              BorderSide(width: 0, color: Colors.transparent)),
                      contentPadding: EdgeInsets.symmetric(vertical: 0),
                    ),
                    style: TextStyle(
                        color: ThemeColors.colorType4,
                        fontSize: ScreenAdapter.sp(32)),
                    // focusNode: focusNode,
                    keyboardType: keyboardType != null ? keyboardType : null,
                    textAlign: TextAlign.left,
                    maxLines: 1,
                    autocorrect: true,
                    // enabled: inputOnly,
                    inputFormatters: inputFormatters ?? [],
                    readOnly: !inputOnly,
                    autofocus: false,
                    controller: TextEditingController(text: content ?? ""),
                    // validator: (val) {
                    //   if (val.length <= 0) {
                    //     ToastUtils.show(placeholder);
                    //     return "";
                    //   }
                    //   return null;
                    //   return val.length <= 0 ? placeholder : null;
                    // },

                    onChanged: onChangeCallback,
                    onSaved: onSaveCallback,
                  )),
                  !inputOnly
                      ? Image.asset(
                          RPath.imgPath + "icon_person_right_arrow.png",
                          width: ScreenAdapter.width(30),
                          height: ScreenAdapter.width(30),
                        )
                      : SizedBox(),
                ],
              ),
            ))
          ],
        ));
  }
}
